<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\captcha\Captcha;

class Recommend extends Base
{
    public function index()
    {
        //
        $list=db('Recommend')->select();
        $this->assign('list',$list);
        return  $this->fetch();
    }
	//验证码
	public function add(){
        
        if( Request()->isPost() ){
            $inputdata = input();
            $data = [];
            $data["name"] = $inputdata["name"];
            $data["age"] = $inputdata["age"];
            $data["paiban"] = $inputdata["paiban"];
            $data["type"] = $inputdata["type"];
            $data["zhanshi"] = $inputdata["zhanshi"];
            $res = db( "Recommend" )->insert( $data );
            if( $res ){
                $this->success( "添加成功", url( "Recommend/index" ) );
            }else{
                $this->error( "添加失败" );
            }
        }else{
            return $this->fetch();
        }
    }
    public function update()
    {
        //
        $field = 'id,name,age,paiban,type,zhanshi';
        $id=input('id');
        $data = db('Recommend')->where('id',$id)->find();
        $this->assign('data',$data);
        if(request()->isPost())
        {
            $inputdata = input();
             $data["name"] = $inputdata["name"];
            $data["age"] = $inputdata["age"];
            $data["paiban"] = $inputdata["paiban"];
            $data["type"] = $inputdata["type"];
            $data["zhanshi"] = $inputdata["zhanshi"];
            $res = db( "Recommend" )->field($field)->where('id',$inputdata['id'])->update( $inputdata );
            if( $res ){
                $this->success( "修改成功",url('Recommend/index') );
            }else{
                $this->error( '修改失败' );
            }
        }
        return $this->fetch();
    }
    public function delete($id)
    {
        $id = input('id');
        $res=db('Recommend')->where('id',$id)->delete();
        if($res)
        {
                $this->success('删除成功','Recommend/index');
        }
        else
        {
            $this->error('删除失败');
        }
    }

    //状态开启关闭
    public function state(){
        $status = input("status");
        $id = input("id");
        $where = array();
        $where["id"] = $id;
        $rst = db("Recommend")->update(["status"=>$status]);
        if($rst){
            $this->success("修改成功");
        }else{
            $this->error("修改失败");
        }
    }
}